var HOST = 'jieyan.xyitech.com';  
// var PORT = 10887;
var PORT = 10887;
const net = require('net');

function parse (json) {
  return JSON.parse(json, (key, value) => {
      return value && value.type === 'Buffer'
        ? Buffer.from(value.data)
        : value;
    });
}

function appendHead (msg) {
  var buf = new Buffer(4 + msg.length);
  buf.writeUInt32LE(msg.length, 0)
  msg.copy(buf, 4);
  return buf
}
var pos = {
    y: 119.797517 * 1e7,
    x: 30.5228416 * 1e7,
    z: 10* 1e3,
  }
  var v = {
    x: 200,
    y: 0,
    z: 0,
  }
var msg = {1:
  [1,30,0,1,0,1],
  2:[2,30,1490237931,11500,0,0,0,0,0,0,0,0,0,819.3,0,200,199,0,0,10297],
  4:[4,30,1490237931,-0.025,0,0,270],
  3:[3,30,1490237931,16.524572372437,-1.1581645011902,-63.066646575928,0.00065353908576071,-0.002983852988109,-0.010271010920405,16.566585540771],
 5:[5,30,1490237931,3,pos.x,pos.y,pos.z,0,0,0,0,10]
}
const client = net.createConnection({port: PORT, host: HOST}, () => {
  //'connect' listener
    
    var dis = 0
  console.log('connected to server!');
  setInterval(function () {
    // var body = new Buffer(10);
    // body.writeInt32LE(6, 0)
    // body.writeUInt8(254, 4);
    // body.writeInt32LE(30, 5);
    // body.writeUInt8(2, 9);
    // // console.log("body", body)
    // client.write(body);

    var body = new Buffer(13);
    body.writeInt32LE(9, 0)
    body.writeUInt8(1, 4);
    body.writeUInt32LE(18, 5);
    body.writeUInt8(0, 9);
    body.writeUInt8(1, 10);
    body.writeUInt8(72, 11);
    body.writeUInt8(1, 12);
    client.write(body);

    var body = new Buffer(45);
    body.writeInt32LE(41, 0)
    body.writeUInt8(2, 4);
    body.writeUInt32LE(18, 5);
    body.writeUInt32LE(new Date().getTime() / 1000 | 0, 9);
    body.writeUInt16LE(50000 * Math.random(), 13);
    body.writeUInt16LE(0, 15);
    body.writeUInt16LE(0, 17);
    body.writeUInt16LE(0, 19);
    body.writeUInt16LE(0, 21);
    body.writeUInt16LE(0, 23);
    body.writeUInt16LE(0, 25);
    body.writeUInt16LE(0, 27);
    body.writeUInt16LE(0, 29);
    body.writeUInt16LE(0, 31);
    body.writeInt16LE(8193, 33);
    body.writeUInt8(100, 34);
    body.writeUInt8(200, 35);
    body.writeUInt8(199, 36);
    body.writeUInt16LE(1, 37);
    body.writeUInt16LE(100, 39);
    client.write(body);

    var body = new Buffer(41);
    body.writeInt32LE(37, 0)
    body.writeUInt8(3, 4);
    body.writeUInt32LE(18, 5);
    body.writeUInt32LE(new Date().getTime() / 1000 | 0, 9);
    body.writeFloatLE(16.524572372437, 13);
    body.writeFloatLE(-1.1581645011902, 17);
    body.writeFloatLE(-63.066646575928, 21);
    body.writeFloatLE(0.00065353908576071, 25);
    body.writeFloatLE(-0.002983852988109, 29);
    body.writeFloatLE(-0.010271010920405, 33);
    body.writeFloatLE(16.566585540771, 37);
    client.write(body);


    var body = new Buffer(23);
    body.writeInt32LE(19, 0)
    body.writeUInt8(4, 4);
    body.writeUInt32LE(18, 5);
    body.writeUInt32LE(new Date().getTime() / 1000 | 0, 9);
    body.writeInt32LE(10000, 13);
    body.writeInt16LE(0, 17);
    body.writeInt16LE(0, 19);
    body.writeUInt16LE(10, 21);
    client.write(body);


    var body = new Buffer(35);
    body.writeInt32LE(31, 0)
    body.writeUInt8(5, 4);
    body.writeUInt32LE(18, 5);
    body.writeUInt32LE(new Date().getTime() / 1000 | 0, 9);
    body.writeUInt8(3, 13);
    body.writeInt32LE(pos.x, 14);
    body.writeInt32LE(pos.y, 18);
    body.writeInt32LE(pos.z, 22);
    body.writeUInt16LE(150, 26);
    body.writeUInt16LE(254, 28);
    body.writeUInt16LE(0.57, 30);
    body.writeUInt16LE(271.31, 32);
    body.writeUInt8(6, 34);

    client.write(body);
    pos.x += v.x
    pos.y += v.y
    pos.z += v.z
    // dis += v.x * 10000
    // console.log("body", body)
    // client.write(body);

    // var body = new Buffer(31);
    // body.writeInt32LE(27, 0)
    // body.writeUInt8(108, 4);
    // body.writeUInt8(30, 5);
    // body.writeUInt8(1, 6);
    // body.writeUInt8(0, 7);
    // body.writeUInt16LE(1, 8);
    // body.writeUInt8(1, 10);
    // body.writeInt32LE(1123, 11)
    // body.writeInt32LE(2123, 15)
    // body.writeInt32LE(3123, 19)
    // body.writeInt32LE(4123, 23)
    // body.writeInt32LE(800, 27)
    // // console.log("body", body)
    // client.write(body);
    // client.write("asdasda")
    // client.write('10{"id":30}');
  }, 2000)
  
  // client.end();
});
client.on('data', (data) => {
  console.log("receiving", JSON.stringify(data));
  // console.log("receiving", data.readUInt8(1));
  // console.log("receiving", data.readUInt8(0));
  // client.end();
});
client.on('end', () => {
  console.log('disconnected from server');
});